{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from scipy.stats import ttest_ind\n",
    "from scipy.stats import shapiro\n",
    "from  utils import column_letter_to_index\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "from scipy.stats import mannwhitneyu\n",
    "from scipy.stats import linregress\n",
    "#数据读取\n",
    "data_path = '../data/raw_data/334份 按选项序号 汇总变量后.xlsx'\n",
    "\n",
    "df = pd.read_excel(data_path)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kruskal-Wallis统计量： 28.2047364674937\n",
      "p值： 7.506185524331548e-07\n"
     ]
    }
   ],
   "source": [
    "# 性别B 户籍 D\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 假设您的数据存储在一个名为df的DataFrame中\n",
    "\n",
    "# 获取列的序号\n",
    "\n",
    "# 用于离散变量和电子健康素养的关系\n",
    "education_col_index = column_letter_to_index('F')#X\n",
    "ele_health_col_index = column_letter_to_index('AT')#Y\n",
    "\n",
    "#-----------------------------------------------------------------\n",
    "# 通过序号获取列名\n",
    "ele_health_col_name = df.columns[ele_health_col_index]\n",
    "education_col_name = df.columns[education_col_index]\n",
    "\n",
    "columns_to_extract = {\n",
    "         df.columns[ele_health_col_index]: 'health_literacy',\n",
    "       df.columns[education_col_index]: 'education',\n",
    "    }\n",
    "processed_df = df[list(columns_to_extract.keys())].rename(columns=columns_to_extract)\n",
    "\n",
    "\n",
    "# 假设您的数据存储在名为data的DataFrame中，包含了多行多列的数据\n",
    "# 选择包含电子健康素养和受教育程度的两列数据\n",
    "selected_data = processed_df[['health_literacy', 'education']]\n",
    "\n",
    "\n",
    "\n",
    "# 将数据按照受教育程度进行分组\n",
    "groups = selected_data.groupby('education')['health_literacy'].apply(list).values\n",
    "\n",
    "\n",
    "\n",
    "# 执行Kruskal-Wallis检验\n",
    "result = stats.kruskal(*groups)\n",
    "\n",
    "# 打印Kruskal-Wallis检验结果\n",
    "print(\"Kruskal-Wallis统计量：\", result.statistic)\n",
    "print(\"p值：\", result.pvalue)\n",
    "\n",
    "\n",
    "# # 创建一个模型对象\n",
    "# model_formula = 'ele_health_col_index ~ education_col_index'\n",
    "\n",
    "# model = ols(model_formula, data=processed_df).fit()\n",
    "\n",
    "# # 执行方差分析\n",
    "# anova_table = sm.stats.anova_lm(model, typ=1)\n",
    "\n",
    "# # 打印方差分析结果\n",
    "# print(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "皮尔逊相关系数: -0.23137544862911075\n",
      "斯皮尔曼相关系数: -0.2667422161978675\n"
     ]
    }
   ],
   "source": [
    "pearson_corr = df.iloc[:,education_col_index].corr(df.iloc[:,ele_health_col_index], method='pearson')\n",
    "print(\"皮尔逊相关系数:\", pearson_corr)\n",
    "\n",
    "spearman_corr = df.iloc[:,education_col_index].corr(df.iloc[:,ele_health_col_index], method='spearman')\n",
    "print(\"斯皮尔曼相关系数:\", spearman_corr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "whj",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
